package com.wy.tingshu.service;

import com.wy.tingshu.data_object.TidToCountDO;
import com.wy.tingshu.data_object.TrackDO;
import com.wy.tingshu.repository.RelationRepo;
import com.wy.tingshu.repository.TrackRepo;
import com.wy.tingshu.util.Log;
import com.wy.tingshu.view_object.ListTrackView;
import com.wy.tingshu.view_object.PaginationView;
import com.wy.tingshu.view_object.StudioTrackView;
import com.wy.tingshu.view_object.UserVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 美女
 * @date 2022/07/16 13:47
 **/
public class TrackService {
    private final TrackRepo trackRepo=new TrackRepo();
    private final RelationRepo relationRepo=new RelationRepo();
    public StudioTrackView listOfUser(UserVO userVO, int page) {
        StudioTrackView resultView=new StudioTrackView();
        resultView.currentUser=userVO;
        //0.规定每页共有5条信息，将页面信息先查出来并做判断
        int countPerPage=5;
        Log.println("每页的音频个数是：："+countPerPage);
        int count=trackRepo.selectCountByUid(userVO.uid);//通过登录用户的uid查找总该用户的总音频数
        Log.println("属于当前用户的音频总数是："+count);
        if(count==0){//没有音频时特殊处理(否则sql有问题，page-1会是负数)
            resultView.pagination=new PaginationView();
            resultView.pagination.countPerPage=countPerPage;
            resultView.pagination.currentPage=1;//当前页设置为第一页
            resultView.pagination.totalPage=0;
            resultView.trackList=new ArrayList<>();
            return resultView;
        }
        //音频数量count为0不执行下面逻辑，否则:count=0,则totalPage=0,则所有page>totalPage,if(page>totalPage){page=totalPage=0}，offset中page-1=负数，执行查询就会出问题
        int totalPage=count/countPerPage;//总页数=（所有内容数/每一页的内容数）并向上取整
        if(count%countPerPage>0){
            //向上取整
            totalPage++;
        }
        Log.println("共有页数："+totalPage);
        //针对页面超出范围的值，调整成相对合理的合法值，这样就不用大量的抛出异常或错误提示了
        if(page<1){
            Log.println("页面信息小于最小值，调整为1");
            page=1;
        }
        if(page>totalPage){
            Log.println("页面信息大于最大值/最大页数，调整为最大值");
            page=totalPage;
        }
        int offset=(page-1)*countPerPage;


        //1.从数据库tracks表中查询
        List<TrackDO> list=trackRepo.selectListByUidLimitOffset(userVO.uid,countPerPage,offset);
        //page=1代表第一页。limit 20 offset 0;page==1   limit 20 offset 20;page==2  limit 20 offset 40;page==3


        Log.println("从数据库表中查询到的结果:"+list);
        if(!list.isEmpty()){
            Log.println("结果不为空，所以需要继续查询关联数量");
            //2.根据结果查询每个track对应的refCount
            //2.1先从list中得到每一个tid
            List<Integer> tidList=new ArrayList<>();
            for (TrackDO trackDO:list){
                tidList.add(trackDO.tid);
            }
            //2.2拿着tidList从relations表中得到需要的结果Map<tid,refCount>
            Map<Integer,Integer> tidToRefCountMap=new HashMap<>();
            List<TidToCountDO> tidToCountDOList=relationRepo.selectCountByTidListGroupByTidList(tidList);
            Log.println("查询relations表得到的结果："+tidToCountDOList);
            for (TidToCountDO tidToCountDO:tidToCountDOList){
                tidToRefCountMap.put(tidToCountDO.tid, tidToCountDO.refCount);
            }
            Log.println("tid to ref count map:"+tidToRefCountMap);

            //3.将TrackDO对象转换为ListTrackView
            List<ListTrackView> resultList=new ArrayList<>();
            for (TrackDO trackDO:list){
                ListTrackView view=new ListTrackView(trackDO,tidToRefCountMap.getOrDefault(trackDO.tid,0));//调用ListTrackView中的构造方法转化
                resultList.add(view);
            }
            Log.println("转换成的准备输出的结果："+resultList);
            resultView.trackList=resultList;
        }else {
            Log.println("结果为空，关联数量无需查询");
            resultView.trackList=new ArrayList<>();
        }


        //4.查询出页面信息

        PaginationView paginationView=new PaginationView();
        paginationView.countPerPage=countPerPage;//每一页内容数
        paginationView.currentPage=page;//当前页面是第几页
        paginationView.totalPage= totalPage;//总页面数
        resultView.pagination=paginationView;

        return resultView;
    }
}
